<template>
  <div>
    <el-dialog
      class="my_dialog"
      v-bind="$attrs"
      v-on="$listeners"
      v-draggable
      width="600px"
      :visible="visible"
      @open="onOpen"
      @close="onClose"
      :title="title"
      :close-on-click-modal="false"
    >
      <el-row :gutter="15">
        <el-form
          ref="elForm"
          :model="formData"
          :rules="rules"
          size="medium"
          label-width="100px"
        >
          <el-form-item label="库位编码" prop="code">
            <el-input
              v-model="formData.code"
              placeholder="请输入库位编码"
              clearable
              :style="{ width: '100%' }"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="库位名称" prop="name">
            <el-input
              v-model="formData.name"
              placeholder="请输入库位名称"
              clearable
              :style="{ width: '100%' }"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="所属仓库" prop="wareHouseId">
            <el-select
              v-model="formData.wareHouseId"
              placeholder="请选择所属仓库"
              clearable
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="(item, index) in wareHouseIdOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="长度" prop="length">
            <el-input
              v-model="formData.length"
              placeholder="请输入长度"
              clearable
              :style="{ width: '100%' }"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="宽度" prop="width">
            <el-input
              v-model="formData.width"
              placeholder="请输入宽度"
              clearable
              :style="{ width: '100%' }"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="高度" prop="height">
            <el-input
              v-model="formData.height"
              placeholder="请输入高度"
              clearable
              :style="{ width: '100%' }"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="最大重量" prop="maxWeight">
            <el-input
              v-model="formData.maxWeight"
              placeholder="请输入最大重量"
              clearable
              :style="{ width: '100%' }"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="最大尺寸" prop="maxSize">
            <el-input
              v-model="formData.maxSize"
              placeholder="请输入最大尺寸"
              clearable
              :style="{ width: '100%' }"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="formData.status"
              placeholder="请选择状态"
              clearable
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="(item, index) in statusOptions"
                :key="index"
                :label="item.dictLabel"
                :value="Number(item.dictValue)"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label-width="110px"
            label="是否默认库位"
            prop="isDefault"
            required
          >
            <el-switch
              v-model="formData.isDefault"
              :active-value="0"
              :inactive-value="1"
            ></el-switch>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="formData.remark"
              type="textarea"
              placeholder="请输入备注"
              :autosize="{ minRows: 4, maxRows: 4 }"
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="onClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  inheritAttrs: false,
  components: {},
  props: ["title", "visible", "item"],
  data() {
    return {
      formData: {
        code: "",
        name: "",
        wareHouseId: "",
        length: "",
        width: "",
        height: "",
        maxWeight: "",
        maxSize: "",
        status: "",
        isDefault: 1,
        remark: "",
      },
      rules: {
        code: [
          {
            required: true,
            message: "请输入库位编码",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入库位名称",
            trigger: "blur",
          },
        ],
        wareHouseId: [
          {
            required: true,
            message: "请选择所属仓库",
            trigger: "change",
          },
        ],
        length: [
          {
            required: true,
            message: "请输入长度",
            trigger: "blur",
          },
        ],
        width: [
          {
            required: true,
            message: "请输入宽度",
            trigger: "blur",
          },
        ],
        height: [
          {
            required: true,
            message: "请输入高度",
            trigger: "blur",
          },
        ],
        maxWeight: [
          {
            required: true,
            message: "请输入最大重量",
            trigger: "blur",
          },
        ],
        maxSize: [
          {
            required: true,
            message: "请输入最大尺寸",
            trigger: "blur",
          },
        ],
        status: [
          {
            required: true,
            message: "请选择状态",
            trigger: "change",
          },
        ],
      },
      //状态
      statusOptions: [],
      //仓库
      wareHouseIdOptions: [],
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async getDictText() {
      let res = await this.getDicts("sys_normal_disable");
      if (res.data != null) {
        this.statusOptions = res.data;
      }
      this.$nextTick(() => {
        if (this.item) {
          this.formData = JSON.parse(JSON.stringify(this.item));
        }
      });
    },
    onOpen() {
      this.getDictText();
    },
    onClose() {
      this.$refs["elForm"].resetFields();
      this.close();
    },
    close() {
      this.$emit("update:visible", false);
    },
    handleConfirm() {
      let form = JSON.parse(JSON.stringify(this.formData));
      console.log(form);
      this.$refs["elForm"].validate((valid) => {
        if (!valid) return;
        this.$emit("Ok", form);
        this.close();
      });
    },
  },
};
</script>
<style>
</style>
